
const { ipcMain, BrowserWindow, Menu, MenuItem } = require('electron')
const path = require('path')
const cssText = `width: 80px; height: 30px; border-radius: 5px; line-height: 30px; text-align: center; background-color: cornflowerblue; position: fixed; bottom: 50px; right: 20px; z-index: 1000; color: #fff; cursor: default`

const saveas = require('./saveAs')

const js = `
  const div = document.createElement('div')
  div.innerHTML = '关闭窗口'
  div.style.cssText = '${cssText}'
  div.addEventListener('click', () => { myApi.close() })
  document.body.appendChild(div)
`
let win;
const open = (url) => {
    win = new BrowserWindow({
        width: 1200,
        height: 600,
        webPreferences: {
            preload: path.resolve(__dirname, '../preload/secordWin.js')
        },
        show: false
    })
    win.loadURL(url)

    win.webContents.executeJavaScript(js).catch((e) => { })
    // win.webContents.openDevTools()
    win.on('ready-to-show', () => {
        win.show()
    })

    // 上下文菜单
    win.webContents.on('context-menu', (e, args) => {
        // console.log(args, '2222222222');
        saveas(args.srcURL)
    })
}

// 打开窗口
ipcMain.handle('on-openSite-event', (e, msg) => {
    open(msg)
})

// 关闭窗口
ipcMain.handle('on-close-event', () => {
    win.close()
})




